package com.fc.h5dispaly.h5display.util;

import android.os.Looper;
import android.util.Printer;

/**
 * Created by fc on 2020/5/14
 * 实现原理：利用主线程的消息队列处理机制，Looper.getMainLooper().setMessageLogging(printer);
 * 在 printer中判断start和end，来获取主线程dispatch该message的开始和结束时间。
 * 并判定该时间超过阈值(如2000毫秒)为主线程卡慢发生，并dump出各种信息，提供开发者分析性能瓶颈
 */
public class BlockDetectByPrinter {
    private static final String START = ">>>>> Dispatching";
    private static final String END = "<<<<< Finished";

    public static void start() {
        Looper.getMainLooper().setMessageLogging(new Printer() {
            @Override
            public void println(String x) {
                if (x.startsWith(START))
                    LogMonitor.getInstance().startMonitor();
                if (x.startsWith(END))
                    LogMonitor.getInstance().removeMonitor();
            }
        });
    }
}